<template>
  <div>
    <div class="goodsindex">
      <el-form ref="form" :model="queryInfo" >
        <!-- 搜索条件 -->
        <el-row :gutter="20" class="goodsindex-queryInfo">
          <!-- 用户账号或者昵称 -->
          <el-col :xs="8" :sm="6" :md="6" :lg="4" :xl="4">
            <el-select  class="goodsindex-queryInfo-li" v-model="queryInfo.user_login_name" filterable size="small" clearable placeholder="请选择用户账号或者昵称">
              <el-option
                  v-for="item in user_login_name"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-col>
          <el-col :xs="6" :sm="4" :md="3" :lg="2" :xl="2">
            <el-button type="primary" class="goodsindex-queryInfo-li" size="small" @click="search()">搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
      <!-- 检索结果 -->
      <el-row :gutter="20" class="goodsindex-list">
        <el-col :span="24">
          <el-table
              :data="tableData"
              border
              size='small'
              style="width: 100%">
            <el-table-column
                prop="user_id"
                label="用户id"
                width="80">
            </el-table-column>
            <el-table-column
                prop="user_name"
                label="用户名称(昵称)"
                width="120">
            </el-table-column>
            <el-table-column
                prop="user_login"
                label="用户登录账号"
                width="180">
            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
            >
              <template v-slot:default="scope">
                <el-button type="text" size="small" @click="editForm(scope.$index,scope.row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!-- 分页 -->
      <el-row :gutter="20" class="goodsindex-list">
        <el-col :span="24" class="goodsindex-page-box">
          <el-pagination

              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryInfo.page"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="queryInfo.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="queryInfo.total">
          </el-pagination>
        </el-col>
      </el-row>
    </div>
    <UserMenu :visible-child.sync="visible" :childFrom="UserForm" :table-checkbox="TableCheckbox"></UserMenu>
  </div>
</template>

<script>

import {getBaseUsers,userAuth,rolesAll} from "@/api/api.js";
import { path } from "@/utils/path"
import UserMenu from "@/views/auths/UserMenu.vue";
export default {
  name: "UserAuth",
  data(){
    return {
      visible:false,
      tableData: [],
      // 这是账号和名称的搜索框
      user_login_name:[],
      queryInfo:{
        user_login_name:'',
        // user_name: '',
        // user_login: '',
        page: 1 ,
        pageSize: 10,
        total:10
      },
      // 这是给子组件传递的参数
      UserForm:{
        user_id: -1,
        user_name: '',
        user_login: '',
      },
      TableCheckbox:[
        //   这里是传递给新增组件和修改组件的，避免出现点击新增和修改时候还要请求一次后台
      ],
    }
  },
  components:{
    UserMenu
  },
  methods:{
    handleSizeChange(pageSize){
      // console.log('1',pageSize);
      this.users([],this.queryInfo.page,pageSize);

    },
    handleCurrentChange(page){
      // console.log('2',page);
      this.users([],page,this.queryInfo.pageSize);
    },
    search(){
      this.users(this.queryInfo,this.queryInfo.page,this.queryInfo.pageSize);
    },
    addForm(){
      this.visible = true;
    },
    editForm(index,value){
      this.visible = true;
      // console.log(index,value);
      this.UserForm = value;
    },
    roles(data = null){
      // 因为我要的是全部的角色数据,但是原本的接口有分页所以这样写
      rolesAll(data,1,10000).then((res) =>{
        if (res.code === 200){
          // console.log("resssssss",res);
          this.TableCheckbox = res.data.data.map(item => ({ role_id: item.role_id, role_name: item.role_name }));

          // console.log("this.TableCheckbox",this.TableCheckbox)
        }
      }).catch(error =>{
        this.$message({
          type:'error',
          message:error.message || 'Error'
        });
      })
    },
    getUsers(){
      getBaseUsers({ is_delete: 0 }).then((res) =>{
        // console.log("getUsers",res)
        if (res.code === 200){
          let options = [
            {
              label: '',
              value: ''
            }
          ];
          this.$.each(res.data,(index,values)=>{
            options.push({
              label:values.user_login + '---' + (values.user_name !== '' ? values.user_name : "???"),
              value:values.user_id
            })
          })
          this.user_login_name = options;
          // console.log('user_login_name',this.user_login_name);
        }
      }).catch(error =>{
        this.$message({
          type:'error',
          message:error.message || 'Error'
        });
      })
    },
    // 查询用户数据
    users(data,page,pageSize){
      userAuth(data,page,pageSize).then((res) =>{
        // console.log('res123',res)
        if (res.code === 200){
          // 这个分页的三条数据是为了防止分页器添加或者修改数据之后产生的bug,尤其是选择了多个分页条数之后
          this.queryInfo.total = res.data.total;
          // current_page	当前页码
          this.queryInfo.page  = res.data.current_page;
          // per_page	每页条数
          this.queryInfo.pageSize  = res.data.per_page;
          this.tableData = res.data.data;
        }
      }).catch(error =>{
        // console.error('error',error)
        // alert(error.message || 'Error')
        this.$message({
          type:'error',
          message:error.message || 'Error'
        });
      })
    },
  },
  created(){
    this.users([],this.queryInfo.page,this.queryInfo.pageSize);
    this.getUsers();
    this.roles();
  }
}
</script>

<style scoped>

</style>